package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Certificate;
import com.hs.service.CertificateService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("certificate")
public class CertificateController {
    @Autowired
    private CertificateService certificateService;
    @PostMapping
    public R addCertificate(@RequestBody Certificate certificate) {
        Integer i = certificateService.addCertificate(certificate);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("失败");
        }
    }
    @DeleteMapping("{id}")
    public R deleteCertificate(@PathVariable("id") Integer id) {
        Integer i = certificateService.deleteCertificate(id);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("失败");
        }
    }
    @PutMapping
    public R updateCertificate(@RequestBody Certificate certificate) {
        Integer i = certificateService.updateCertificate(certificate);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("失败");
        }
    }
    @GetMapping("{id}")
    public R getCertificateById(@PathVariable("id") Integer id) {
        Certificate certificateById = certificateService.getCertificateById(id);
        return R.ok(certificateById);
    }
    @GetMapping("page")
    public R getCertificateByPage(Certificate certificate,@RequestParam(defaultValue = "1",required = false) Integer pageNum,
                                  @RequestParam(defaultValue = "5",required = false) Integer pageSize) {
        PageInfo<Certificate> certificateByPages = certificateService.getCertificateByPages(certificate,pageNum,pageSize);
        return R.ok(certificateByPages);
    }
}
